{extend name="base/base" /}
{block name="main"}
<style>
.layui-table-cell {
height: 80px;
line-height: 80px;
}
</style>
<form class="layui-form">
    <blockquote class="layui-elem-quote quoteBox">
        <form class="layui-form">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="text" class="layui-input searchVal" placeholder="请输入名称" />
                </div>
                <a class="layui-btn search_btn" data-type="reload">搜索</a>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-danger layui-btn-normal adduser_btn">添加</a>
            </div>
        </form>
    </blockquote>
    <table id="usersList" lay-filter="usersList"></table>
    <!--操作-->
    <script type="text/html" id="usersListBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    </script>
</form>
{/block}
{block name="script"}
<script>
    layui.use(['form','layer','table'],function(){
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery, table = layui.table;

        //用户列表
        var tableIns = table.render({
            elem: '#usersList',
            url : "{:url('/admin/banner-list')}",
            cellMinWidth : 95,
            page : true,
            limit:10,
            limits:[5,10,15,20],
            height : "full-125",
            id:'usersListTable',
            cols : [[
                {field: 'id', title: 'ID', width:100, align:"center"},
                {field: 'title', title: '名称'},
                {field: 'image', title: '图标', templet:function(d){
                     if(d.image==null || d.image == '')
                             return ""
					if(d.file_type== 'video/mp4'){
						return "<video src="+d.image+" controls height='80px' class=''></video>";
					}else{		 
                     return "<img src="+d.image+" height='30px' class=''/>";
					}
                }},
                {
                    field: 'is_show', title: '是否显示', align: 'center', width: 100, templet: function (d) {
                        if (d.is_show == 1) {
                            return '<input type="checkbox" name="is_show"  checked="checked"  lay-filter="is_show" value="' + d.id + '" lay-skin="switch" lay-text="是|否" ' + d.is_show + ' >'
                        } else {
                            return '<input type="checkbox" name="is_show" lay-filter="is_show" value="' + d.id + '" lay-skin="switch" lay-text="是|否" ' + d.is_show + ' >'
                        }
                    }
                },
                {field: 'addtime', title: '添加时间',  align:'center',templet:function(d){ return (d.create_time)?layui.util.toDateString(d.create_time * 1000, "yyyy-MM-dd HH:mm:ss"):''}},
                {title: '操作', width:270, templet:'#usersListBar',fixed:"right",align:"center"}
            ]]
        });

        //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
        $(".search_btn").on("click",function(){
            table.reload("usersListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        });
        
        
        //修改用户组状态
        form.on('switch(is_show)', function (data) {
            var index = layer.msg('修改中，请稍后', {icon: 16, time: false, shade: 0.8});
            setTimeout(function () {
                layer.close(index);
                var is_show = data.elem.checked ? 1 : 0;
                id = data.elem.value;
                $.post("{:url('/admin/banner-bannerdisable')}", {id: id, is_show: is_show}, function (data) {
                    var icon = 5;
                    if (data.code) {
                        icon = 6;
                    }
                    layer.msg(data.msg, {icon: icon, time: 1500});
                });
            }, 500);
        });
        
        //添加用户
        function add(){
            var index = layui.layer.open({
                title : "添加轮播",
                type : 2,
                content : "{:url('/admin/banner-edit')}",
                success : function(layero, index){
                    var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                }
            })
            layui.layer.full(index);
            window.sessionStorage.setItem("index",index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        }
        //编辑用户
        function edit(id){
            var index = layui.layer.open({
                title : "编辑轮播",
                type : 2,
                content : "{:url('/admin/banner-edit')}"+'?id='+id,
                success : function(layero, index){
                    var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                }
            })
            layui.layer.full(index);
            window.sessionStorage.setItem("index",index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(window.sessionStorage.getItem("index"));
            })
        }

        $(".adduser_btn").click(function(){
            add();
        })

        //列表操作
        table.on('tool(usersList)', function(obj){
            var layEvent = obj.event, data = obj.data;
            if(layEvent === 'edit'){ //编辑
                edit(data.id);
            } else if(layEvent === 'del'){ //删除
                layer.confirm('确定删除操作？',{icon:3, title:'提示信息'},function(index){
                    $.post("{:url('/admin/banner-del')}",{id:data.id},function(data){
                        var icon=5;
                        if(data.code){
                            icon=6;
                        }
                        layer.msg(data.msg, {icon:icon,time: 1500}, function () {
                            if(data.code){
                                obj.del();
                            }
                        });
                    })
                });
            }else if(layEvent == 'giftlog'){
                window.location.href = '/admin/gift-log.html?id='+data.id;
            }
        });

    })
</script>
{/block}
